<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="榜单成员uid" prop="memberUid">
      <el-input v-model="dataForm.memberUid" placeholder="榜单成员uid"></el-input>
    </el-form-item>
    <el-form-item label="创建人uid" prop="createUserUid">
      <el-input v-model="dataForm.createUserUid" placeholder="创建人uid"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="点赞类型(目前默认为1 点赞 )
1  点赞
0  点踩" prop="praiseType">
      <el-input v-model="dataForm.praiseType" placeholder="点赞类型(目前默认为1 点赞 )
1  点赞
0  点踩"></el-input>
    </el-form-item>
    <el-form-item label="被点赞/点踩的用户uid" prop="toUserUid">
      <el-input v-model="dataForm.toUserUid" placeholder="被点赞/点踩的用户uid"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          memberUid: '',
          createUserUid: '',
          remark: '',
          status: '',
          createTime: '',
          updateTime: '',
          praiseType: '',
          toUserUid: ''
        },
        dataRule: {
          memberUid: [
            { required: true, message: '榜单成员uid不能为空', trigger: 'blur' }
          ],
          createUserUid: [
            { required: true, message: '创建人uid不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          praiseType: [
            { required: true, message: '点赞类型(目前默认为1 点赞 )
1  点赞
0  点踩不能为空', trigger: 'blur' }
          ],
          toUserUid: [
            { required: true, message: '被点赞/点踩的用户uid不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/rank/praiserecord/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.memberUid = data.praiseRecord.memberUid
                this.dataForm.createUserUid = data.praiseRecord.createUserUid
                this.dataForm.remark = data.praiseRecord.remark
                this.dataForm.status = data.praiseRecord.status
                this.dataForm.createTime = data.praiseRecord.createTime
                this.dataForm.updateTime = data.praiseRecord.updateTime
                this.dataForm.praiseType = data.praiseRecord.praiseType
                this.dataForm.toUserUid = data.praiseRecord.toUserUid
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/rank/praiserecord/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'memberUid': this.dataForm.memberUid,
                'createUserUid': this.dataForm.createUserUid,
                'remark': this.dataForm.remark,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'praiseType': this.dataForm.praiseType,
                'toUserUid': this.dataForm.toUserUid
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
